<template>
  <div>
    <div class="app-container">
      <div style="padding-bottom: 20px;">
        <el-link type="primary" style="padding-right: 20px;">总数量: {{ this.list.length }}</el-link>
        <el-button
        plain size="small"
          v-waves
          class="filter-item btn"
          type="success"
          icon="el-icon-plus"
          @click="handleAdd()"
        >添加包</el-button>
      </div>
      <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="软件名" min-width="100" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="包名" min-width="100" align="center" >
        <template slot-scope="{row}">
          <span>{{ row.pkg }}</span>
        </template>
      </el-table-column>
     
      <el-table-column
        label="操作"
        align="center"
        min-width="100"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row}">
          <!-- <el-button type="primary" plain size="small" @click="handleUpdate(row)">编辑</el-button> -->
          <el-button type="danger" plain size="small" @click="handleDel(row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    </div>

    

<!-- 弹窗 -->
    <el-dialog
      title="sad"
      :visible.sync="updated"
      :width="dialogWidth"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="120px"
        class="fullwidth"
        enctype="multipart/form-data"
      >
      <el-row v-for="(value, index) in temp.pkgs" :key="index">
          <el-col :span="9"><div class="">

            <el-form-item label="软件名"  label-width="80px" :key="index"
              :prop="'pkgs[' + index + '].name'"
              :rules="{required: true, message: '软件名不能为空', trigger: 'blur'}"
            >
              <el-input v-model.trim="value.name" placeholder="软件名" />

            </el-form-item>

          </div></el-col>
          <el-col :span="11"><div class="">

            <el-form-item label="包名"  label-width="60px":key="index"
              :prop="'pkgs[' + index + '].pkg'"
              :rules="{required: true, message: '包名不能为空', trigger: 'blur'}"
            >
              <el-input v-model.trim="value.pkg" placeholder="包名" />
            </el-form-item>
            
          </div></el-col>
          <el-col :span="4"><div class="" style="line-height: 40px;">
            <el-button style="margin-left: 20px;" @click="delidList(index)" icon="el-icon-minus" circle size="mini"></el-button>
          </div></el-col>
        </el-row>
        <el-button  @click="addAdvId" icon="el-icon-plus" circle ></el-button>

        <div style="text-align: right">
          <el-button plain size="small" @click="updated = false">取消</el-button>
          <el-button
           plain size="small"
            type="primary"
            :loading='addloading'
            @click="handadd()"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getInstallPkg, createInstallPkg, delInstallPkg} from "@/api/adv_admin/config.js"
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { Message, MessageBox } from "element-ui";
export default {
    'name': 'LandingPage',
    components: { Pagination },
    directives: { waves },
    data() {
        return { 
            imageUrl: '',
            tableKey: 0,
            listLoading: false,
            addloading: false,
            dialogWidth: '0px',
            list: [],
            total: 0,
            listQuery: {
                page: 1,
                limit: 20,
                temp_type: "",
                name: "",
                status: "",
            },
            temp:{
              pkgs: [
                { 
                  pkg: '',
                  name: ''
                }
              ]
            },
            updated: false,
            update: '',
            dialogStatus: '',
        }
    },
    mounted(){
        this.getList()
        this.setDialogWidth()
    },
    methods: {
        setDialogWidth() {
            const val = document.body.clientWidth;
            const def = 600; // 默认宽度
            if (val < def) {
                this.dialogWidth = "100%";
            } else {
                this.dialogWidth = def + "px";
            }
        },
        getList(){
            this.listLoading = true
            getInstallPkg().then(res => {
                if (res.STATUS == 200){
                  this.list = []
                  let data = res.DATA.list
                  for (var key in data){
                    this.list.push({
                      'pkg': key,
                      'name': data[key]
                    })
                  }
                    
                }
                this.listLoading = false
            })
        },
        handleFilter() {
            this.listQuery.page = 1;
            this.getList();
        },
        handleAdd(){
            this.updated = true
            this.temp.pkgs = [
              {pkg: '', name: ''}
            ];
            this.dialogStatus = 'create'
        },
        handadd(){
            this.addloading = true
            this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              let data = this.temp.pkgs
              let pkgs = {}
              for(let index in data){
                pkgs[data[index].pkg] = data[index].name
              }
              pkgs = JSON.stringify(pkgs)
              createInstallPkg({'pkgs': pkgs}).then(res => {
                    if (res.STATUS == 200){
                        Message.success('添加成功')
                        this.updated = false
                        this.getList()
                    }else{
                        
                        Message({
                            message: res.DEC,
                            type: "error",
                            duration: 2000,
                        });
                    }
                    this.addloading = false
                })

            } else {
                console.log('error submit!!');
                this.addloading = false
            }
            }); 
        },
        handleDel(item){
            MessageBox.confirm("确定删除吗", "确认", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
              console.log(item,"------------------")
                delInstallPkg({ pkg: item.pkg}).then((r) => {
                    if (r.STATUS == 200) {
                        Message({
                            message: "删除成功",
                            type: "success",
                            duration: 2000,
                        });
                        this.getList();
                    }
                });
            }).catch(() => {});
            
        },
        addAdvId(){
          this.temp.pkgs.push({
            pkg: null,
            name: null
          })
        },
        delidList(index){
          this.temp.pkgs.splice(index, 1)
        },
    }
}
</script>
<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.push {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin-left: 5px;
}
.pageHidden {
  display: none;
}
.btn {
  margin-left: 0;
}
.fullwidth {
  width:100%;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .fullwidth {
    width: 100%;
    margin-left: 0;
  }
}

.order_1{
  padding: 2px;
  border-radius: 4px;
  border: 2px solid #409EFF;
}
.order_2{
    padding: 2px;
  border-radius: 4px;
  border: 2px solid #7e7d7d;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 330px;
    /* height: 180px; */
    /* line-height: 178px; */
    text-align: center;
  }
  .avatar {
    width: 330px;
    /* height: 178px; */
    display: block;
  }
  
</style>